/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dbsucxentW.storageW.parserW;

/**
 * An object in charge of calculating the DeweyOrderSum of each leaf node in the
 * data source given that the leaves are passed in the document order
 * @author truongbaquan
 */
public class DeweyOrderSumCalculator {
    private DocumentSummary summary;
    private long[] sum;

    public DeweyOrderSumCalculator(DocumentSummary summary) {
        this.summary = summary;
        this.sum = new long[summary.getMaxDepth() + 1];
        for (int i = 0; i < sum.length; i++) {
            sum[i] = 0;
        }
    }

    public long calculate(int branchOrder) {
        if (branchOrder > 0) {
            long deweyOrdSum = sum[branchOrder] + summary.getRValue(branchOrder);
            int q;
            for (q = branchOrder; q <= summary.getMaxDepth(); q++) {
                sum[q] = deweyOrdSum;
            }
            return deweyOrdSum;
        }
        else return 0;
    }
}
